<?php
namespace app\index\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\Request;

class Login extends Base
{
     public function initialize() {
        
        //已登录直接跳转
        $account = session('userAccount');
        if($account && $account['id']) {
       
             return alert('登录成功！','/index/user',6,3);

        }
    }
    public function index()
    {
   if(request()->isPost()) {

            $data = input('post.');
            


            // 通过用户名 获取 用户相关信息

            $adminData = Db::name('users')->where('username',$data['username'])->find();//一维数组


            if(!$adminData || $adminData['status'] <3 ) {

                return alert('用户不存在，或者此用户未被审核通过','/index/login',5,3);

            }



            // if(!captcha_check($data['verifycode'])) {
            //     // 校验失败
            //     return alert('验证码不正确','index',5,3);
            // }


            if($adminData['password'] !=$this->password_salt($data['password'])) {
                return alert('密码不正确','/index/login',5,3);
            }

            session('userAccount', $adminData);
            
            return alert('登录成功！','/index/user',6,3);


        }else {

            
            return view();

        }
    }
public function kuaijie()
    {
   if(request()->isPost()) {

            $data = input('post.');
            if($data['type']=='wx'){
                $fh= file_get_contents('https://login.xaike.xyz/connect.php?act=login&appid=1000&appkey=0294af8ab5e06e9b2683beb076a43518&type=wx&redirect_uri=http://xaike.xyz/index/login/kuaijier');
              $data=json_decode( $fh); 
           
             return $data;
            }elseif ($data['type']=='qq') {
                 $fh= file_get_contents('https://login.xaike.xyz/connect.php?act=login&appid=1000&appkey=0294af8ab5e06e9b2683beb076a43518&type=qq&redirect_uri=http://xaike.xyz/index/login/kuaijier');
              $data=json_decode( $fh); 
           
             return $data;
            }else{
                
            }
      

    }

    }
 public function kuaijier()
    {
   
    $data1 = input('get.');
    $url1="https://login.xaike.xyz/connect.php?act=callback&appid=1000&appkey=0294af8ab5e06e9b2683beb076a43518&type=" .$data1['type']. "&code=" .$data1['code'];
  
     $fh= file_get_contents($url1);
      $data=json_decode($fh,true);
  
 $userData = Db::name('users')->where('social_uid',$data['social_uid'])->find();//一维数组
 if($userData){//如果存在
      session('userAccount', $userData);
            
            return alert('登录成功！','/index/user',6,3);
 }else{
     $datas['type']=$data['type'];
     $datas['social_uid']=$data['social_uid'];
      $datas['access_token']=$data['access_token'];
      $datas['img']=$data['faceimg'];
      $datas['busername']=$data['nickname'];
       $datas['gender']=$data['gender'];
     $datas['status']="3";
     $datas['username']=$data['social_uid'];
    $datas['token']=md5($data['access_token']);
        $datas['ip']=$data['ip'];
    $res=Db::name('users')->insertGetId($datas);
     $userData = Db::name('users')->where('id',$res)->find();//一维数组
     session('userAccount', $userData);
            
            return alert('登录成功！','/index/user',6,3);
 }
    }

   
    public function register(){
        if(request()->isPost()){

         $data = input('post.');
         
          $userData = Db::name('users')->where('username',$data['username'])->find();//一维数组
          $emailData = Db::name('users')->where('email',$data['email'])->find();//一维数组
          
            if(!$userData) {
            if(!$emailData) {
              $data['time']=time(); 
              $data['status']='3';
              $data['password'] =$this->password_salt($data['password']);
              $data['token'] =$this->password_salt($data['password']);
              $res=Db::name('users')->insertGetId($data);
               $id=Db::name('website')->insertGetId($data);

            if($id){
				return alert('注册成功！跳转至登录页面','/index/login',6,3);
			 }else{
				return alert('注册失败！','/index/login/register',5,3);
			 }
            }else{
              return alert('此邮箱已经被注册，请登录或找回密码');    
            }

            }else{
                return alert('此用户名已经被注册，请登录或找回密码');  
            }
        }
        return view('',[
         
        ]);
    }
  
 

  
}
